import pandas as pd
import numpy as np


C_Z_DICT = {99: 2.33, 95: 1.65, 90: 1.28, 84: 1}
DAYS_IN_MONTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
CSV = 'sale.csv'


class MySafetyStock(object):
    def __init__(self, csv_path: str = CSV) -> None:
        self.df = pd.read_csv(csv_path)
        self.df['lead_time_months'] = self.df['lead_time_days'] / DAYS_IN_MONTHS

    def safety_stock(self, confidence: int) -> int:
        z_score = C_Z_DICT[confidence]
        # 中间变量
        avg_lead_time = self.df['lead_time_months'].mean()
        std_dev_demand = np.std(self.df['demand'])
        std_dev_lead_time = np.std(self.df['lead_time_months'])
        avg_demand = self.df['demand'].mean()

        # 为啥这里avg_lead_time要开平方？
        return z_score * (avg_lead_time ** 0.5 * std_dev_demand + std_dev_lead_time * avg_demand) + avg_demand


if __name__ == '__main__':
    print(MySafetyStock().safety_stock(99))
